/*
 * @(#)UserSyncServiceImpl.java 2011-11-10
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.sync.service.impl;

import org.springframework.stereotype.Service;

import cn.shangjia.common.domain.User;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.shop.dao.AbstractCacheDao.CacheType;
import cn.shangjia.shop.sync.service.AbstractCacheService;

/**
 * 同步服务-用户信息实现.
 *
 * @author <a href="mailto:wangxin@knet.cn">wangxin</a>
 * @version 1.0, 2011-11-11 11:51:15
 * @since 1.0
 */
@Service
public class UserSyncService extends AbstractCacheService<User>{
	
	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.sync.service.AbstractCacheService#insert(java.lang.String, java.lang.String)
	 */
	@Override
	protected void insert(String id, String userid) {
		super.insert(id, userid); 
		String username = getUserName(id);
		if (username != null) 
			super.insert(username, userid);
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.sync.service.AbstractCacheService#update(java.lang.String, java.lang.String)
	 */
	@Override
	protected void update(String id, String userid) {
		super.update(id, userid); 
		String username = getUserName(id);
		if (username != null) 
			super.update(username, userid);
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.sync.service.AbstractCacheService#delete(java.lang.String, java.lang.String)
	 */
	@Override
	protected void delete(String id, String userid) {
		super.delete(id, userid); 
		String username = getUserName(id);
		if (username != null) 
			super.delete(username, userid);
	}
	
	private String getUserName(String id) {
		String key = dao.getKey(CacheType.OBJ, id);
		User user = dao.get(key);
		if (user == null)
			return null;
		String username = user.getUsername();
		if (StringUtils.isEmpty(username))
			return null;
		return username;
	}

}
